package com.ohs.ohsoa.system.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ohs.ohsoa.system.dto.MeetingDto;
import com.ohs.ohsoa.system.entity.Meeting;
import com.ohs.ohsoa.system.entity.Meetingpersons;
import com.ohs.ohsoa.system.mapper.MeetingMapper;
import com.ohs.ohsoa.system.mapper.MeetingpersonsMapper;
import com.ohs.ohsoa.system.vo.MeetingVo;
import com.ohs.ohsoa.system.vo.MeetingpersonsVo;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.exception.BizException;

@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements MeetingService{

    @Resource
    private MeetingMapper meetingMapper;

    @Resource
    private MeetingpersonsMapper meetingpersonsMapper;

    @Resource
    private MessageSource messageSource;

    @Override
    public List<MeetingVo> meetingSelectList(PageParam<MeetingDto> pageParam) {

        //获取对象
        MeetingDto meetingDto = pageParam.getSearchbean();
        //分页
//        PageHelper.startPage(pageParam.getIndexPage(), pageParam.getPageSize());

        return meetingMapper.meetingSelectList(meetingDto);
    }

    @Override
    public MeetingVo meetingSelectByID(MeetingDto meetingDto) {
        //获取会议信息
        MeetingVo meetingVo =  meetingMapper.meetingSelectByID(meetingDto.getMId());
        //获取参与者信息
        List<MeetingpersonsVo> meetingpersonsVos = meetingpersonsMapper.meetingPersonsSelect(meetingDto.getMId());
        meetingVo.setMeetingpersonsVos(meetingpersonsVos);
        return meetingVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean meetingInsert(MeetingDto meetingDto) {

        //判断会议室是否被占用
        Boolean bResult;
        bResult = meetingJudge(meetingDto);
        if (!bResult) {
            throw new BizException("E00012", messageSource.getMessage("E00012", null, null));
        }

        //添加会议
        Meeting meeting = new Meeting();
        BeanUtils.copyProperties(meetingDto, meeting);
        bResult = this.save(meeting);
        if (!bResult){
            return false;
        }

        MeetingpersonsService meetingpersonsService = new MeetingpersonsServiceImpl();
        List<Meetingpersons> meetingpersonsList = new ArrayList<>();
        //添加发起者
        Meetingpersons meetingpersons = meetingpersonsInsert(meeting.getMId(), meetingDto.getMAid(), "1");
        meetingpersonsList.add(meetingpersons);

        //添加必须参加者
        for (int i = 0; i < meetingDto.getMustJoin().size(); i++){
            meetingpersons = meetingpersonsInsert(meeting.getMId(), meetingDto.getMustJoin().get(i), "2");
            meetingpersonsList.add(meetingpersons);
        }

        //添加可参加者
        for (int j = 0; j < meetingDto.getCanJoin().size(); j++){
            meetingpersons = meetingpersonsInsert(meeting.getMId(), meetingDto.getCanJoin().get(j), "3");
            meetingpersonsList.add(meetingpersons);
        }

        try {
            bResult = meetingpersonsService.meetingPersonsInsert(meetingpersonsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!bResult){
            return false;
        }

        return bResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean meetingDelete(MeetingDto meetingDto) {

        //删除会议信息
        return meetingMapper.meetingDelete(meetingDto.getMId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean meetingUpdate(MeetingDto meetingDto) {

        //判断会议室是否被占用
        Boolean bResult = true;

        bResult = meetingJudge(meetingDto);

        if (!bResult) {
            throw new BizException("E00012", messageSource.getMessage("E00012", null, null));
        }

        bResult = meetingMapper.meetingUpdate(meetingDto);
        if (!bResult){
            return false;
        }

        //删除参与者信息
        bResult = meetingpersonsMapper.meetingPersonsDelete(meetingDto.getMId());
        if (!bResult){
            return false;
        }

        MeetingpersonsService meetingpersonsService = new MeetingpersonsServiceImpl();
        List<Meetingpersons> meetingpersonsList = new ArrayList<>();
        //添加发起者
        Meetingpersons meetingpersons = meetingpersonsInsert(meetingDto.getMId(), meetingDto.getMAid(), "1");
        meetingpersonsList.add(meetingpersons);

        //添加必须参加者
        for (int i = 0; i < meetingDto.getMustJoin().size(); i++){
            meetingpersons = meetingpersonsInsert(meetingDto.getMId(), meetingDto.getMustJoin().get(i), "2");
            meetingpersonsList.add(meetingpersons);
        }

        //添加可参加者
        for (int j = 0; j < meetingDto.getCanJoin().size(); j++){
            meetingpersons = meetingpersonsInsert(meetingDto.getMId(), meetingDto.getCanJoin().get(j), "3");
            meetingpersonsList.add(meetingpersons);
        }

        try {
            bResult = meetingpersonsService.meetingPersonsInsert(meetingpersonsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!bResult){
            return false;
        }
        return bResult;
    }

    /**
     * 添加会议参与者
     * @param mid 会议id
     * @param userinfoGuid 用户信息
     * @param mpType 会议类型
     * @return 参与者
     */
    public Meetingpersons meetingpersonsInsert(String mid, String userinfoGuid, String mpType){

        Meetingpersons meetingpersons = new Meetingpersons();
        meetingpersons.setMId(mid);
        meetingpersons.setUserinfoGuid(userinfoGuid);
        meetingpersons.setMpType(mpType);

        //发起者会议设置已读和确认参加
        if (mpType.equals("1")){
            meetingpersons.setMpReadflg("1");
            meetingpersons.setMpSureflg("1");
        }else
        {
            meetingpersons.setMpReadflg("0");
            meetingpersons.setMpSureflg("0");
        }
        meetingpersons.setDelflg("1");

        return meetingpersons;
    }

    /**
     * 判断会议室是否被占用
     * @param meetingDto 会议室信息
     * @return 是否被占用
     */
    public Boolean meetingJudge(MeetingDto meetingDto){

        QueryWrapper<Meeting> queryWrapper = new QueryWrapper<>();
        if (meetingDto.getMId() == null){
            queryWrapper.eq("group_enterprise_guid", meetingDto.getGroupEnterpriseGuid())
                    .eq("mr_id", meetingDto.getMrId())
                    .eq("delflg", "1")
                    .gt("m_btime", meetingDto.getMBtime())
                    .lt("m_btime", meetingDto.getMEtime()).or()
                    .eq("group_enterprise_guid", meetingDto.getGroupEnterpriseGuid())
                    .eq("mr_id", meetingDto.getMrId())
                    .eq("delflg", "1")
                    .gt("m_etime", meetingDto.getMBtime())
                    .lt("m_etime", meetingDto.getMEtime()).or()
                    .eq("group_enterprise_guid", meetingDto.getGroupEnterpriseGuid())
                    .eq("mr_id", meetingDto.getMrId())
                    .eq("delflg", "1")
                    .le("m_btime", meetingDto.getMBtime())
                    .ge("m_etime", meetingDto.getMEtime());
        }else {
            queryWrapper.eq("group_enterprise_guid", meetingDto.getGroupEnterpriseGuid())
                    .eq("mr_id", meetingDto.getMrId())
                    .eq("delflg", "1")
                    .ne("m_id", meetingDto.getMId())
                    .gt("m_btime", meetingDto.getMBtime())
                    .lt("m_btime", meetingDto.getMEtime()).or()
                    .eq("group_enterprise_guid", meetingDto.getGroupEnterpriseGuid())
                    .eq("mr_id", meetingDto.getMrId())
                    .eq("delflg", "1")
                    .ne("m_id", meetingDto.getMId())
                    .gt("m_etime", meetingDto.getMBtime())
                    .lt("m_etime", meetingDto.getMEtime()).or()
                    .eq("group_enterprise_guid", meetingDto.getGroupEnterpriseGuid())
                    .eq("mr_id", meetingDto.getMrId())
                    .eq("delflg", "1")
                    .ne("m_id", meetingDto.getMId())
                    .le("m_btime", meetingDto.getMBtime())
                    .ge("m_etime", meetingDto.getMEtime());
        }

        if (this.count(queryWrapper) == 0){
            return true;
        }

        return false;

    }
}
